package com.yupu.springboot_web.handler;

import cn.hutool.json.JSONUtil;
import com.yupu.springboot_web.result.R;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * 无权访问的
 * 自定义的AccessDeniedHandler，当用户访问无权访问的资源时，返回json格式的数据
 */
@Component
public class MyAccessDeniedHandler implements AccessDeniedHandler {

    @Override
    public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException accessDeniedException) throws IOException, ServletException {
        //返回json，告诉前端: 权限不足
        response.setContentType("application/json");
        response.setCharacterEncoding("utf-8");

        //采用构建器模式，链式编程创建一个R对象
        R result = R.builder().code(401).msg("权限不足").build();

        //hutool工具包，把R对象转成json字符串
        String json = JSONUtil.toJsonStr(result);

        //把json写出去，写出到浏览器客户端
        response.getWriter().write(json);
    }
}
